<template>
  <div class="app-container">
    <h3 class="title">客服管理</h3>
    <!-- <div class="inputBox">
      <el-input v-model="search.orderNumbers" class="input" placeholder="请输入订单号查询" />
      <el-select v-model="search.vipId" class="teacherStatus" placeholder="请选择会员等级">
        <el-option label="泰山美妈" value="1" />
        <el-option label="泰山美妈团" value="2" />
        <el-option label="泰山美妈社" value="3" />
        <el-option label="泰山美妈会" value="4" />
      </el-select>
      <el-input v-model="search.name" class="input" placeholder="请输入购买人查询" />
      <el-input v-model="search.phone" class="input" placeholder="请输购买人手机号查询" />
      <el-date-picker
        v-model="search.startTime"
        class="startDate"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择开始日期"/>
      <el-date-picker
        v-model="search.endTime"
        class="endDate"
        type="date"
        value-format="yyyy-MM-dd"
        placeholder="选择结束日期"/>
      <el-button type="primary" class="ml10" @click="toSearch">查询</el-button>
      <el-button type="primary" @click="toReset">重置</el-button>
    </div> -->
    <!-- <BankView ref="bankDialog" :visible="visible" :dialog-type="'order'" @isDialog="isDialog" />
    <Dialog ref="dialog" :showdialog="showDialog" :bankname="bankName" :tid="tid" :coursesection="courseSection" :orderid="orderId" :bankid="bankId" :type="type" @isDialog="isDialog" />
    <div class="inputBox">
      <el-input v-model="search.orderNum" class="input" placeholder="请输入订单编号查询" width="95"/>
      <el-input v-model="search.title" class="input" placeholder="请输入课程标题查询" />
      <el-input v-model="search.buyer" class="input" placeholder="请输入购买人人查询" />
      <el-input v-model="search.buyPhone" class="input" placeholder="请输入购买人手机号查询" />
      <el-date-picker v-model="search.startTime" class="startDate" type="date" placeholder="选择开始日期"/>
      <el-date-picker v-model="search.endTime" class="endDate" type="date" placeholder="选择结束日期"/>
      <el-select v-model="search.status" class="teacherStatus" placeholder="请选订单状态">
        <el-option label="已付款" value="100" />
        <el-option label="已提现" value="500" />
        <el-option label="退款中" value="650" />
        <el-option label="已退款" value="700" />
        <el-option label="已完成" value="800" />
      </el-select>
      <el-button type="primary" class="ml10" @click="toSearch()">查询</el-button>
      <el-button type="primary" @click="toReset()">重置</el-button>
    </div> -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="加载中请稍后"
      border
      fit
      highlight-current-row>
      <el-table-column label="客服姓名">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="电话">
        <template slot-scope="scope">
          {{ scope.row.phone }}
        </template>
      </el-table-column>
      <el-table-column label="微信号">
        <template slot-scope="scope">
          {{ scope.row.detailAddress }}
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <span v-if="scope.row.status === '0'">停用</span>
          <span v-else-if="scope.row.status === '1'">启用</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="操作" width="240">
        <template slot-scope="scope">
          <!-- <el-button v-show="permissionShow && permissionBoolean('详情')" size="mini" type="warning" @click="see(scope.$index)">查看详情</el-button>
          <el-button v-show="permissionShow && permissionBoolean('提课程款')" v-if="scope.row.status === 100" size="mini" type="primary" @click="submit(scope.row.oid, scope.row.tid, scope.row.courseSection)">提课程款</el-button>
          <el-button v-show="permissionShow && permissionBoolean('退佣金')" v-else-if="scope.row.status === 500" size="mini" type="success" @click="refund(scope.row.oid, scope.row.tid, scope.row.orderCommission)">退佣金</el-button>
           -->

          <!-- v-show="permissionShow && permissionBoolean('详情')" -->
          <!-- <el-button
            size="mini"
            type="warning"
            @click="see(scope.$index)">详情</el-button> -->
          <el-button
            :disabled="scope.row.status === '1'"
            size="mini"
            type="primary"
            @click="edit(scope.row)">编辑</el-button>
          <el-button
            v-if="scope.row.status === '0'"
            size="mini"
            type="success"
            @click="update(scope.row.id, '1')">启用</el-button>
          <el-button
            v-if="scope.row.status === '1'"
            size="mini"
            type="danger"
            @click="update(scope.row.id, '0')">停用</el-button>
            <!-- <el-button
            size="mini"
            type="success"
          @click="refund(scope.row.oid, scope.row.tid, scope.row.orderCommission)">查看详情</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <!-- <remark :remark-list="remarkList" :remark-visible.sync="remarkVisible" :remark-index="remarkIndex" /> -->
    <!-- <remarkPropt
      ref="remarke"
      :remark-propt-list="remarkProptList"
      :remark-propt-visible.sync="remarkProptVisible"
      :remark-propt-index="remarkProptIndex"
      :remark-type="-1"
      @remarkText="remarkText" /> -->
    <detail :detialvisible.sync="dialogTableVisible" :dialogdata.sync="list" :myindex.sync="detailIndex" :detail-mode="'address'" />
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="fetchData" />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
// import BankView from '@/components/Dialog/BankView'
import detail from '@/components/detail'
import Dialog from '@/components/Dialog'
import { Message } from 'element-ui'
import { getOrderList, selectCard, addRemark, getRemarkPage } from '@/api/order'
import { getLocalUid, setLocalUid, getLocalId, setLocalId } from '@/utils/uid'
import { getPermission } from '@/api/permission'
import { getLocalUserName, setLocalUserName } from '@/utils/uid'
import remark from '@/components/remark'
import remarkPropt from '@/components/remark/remarkPrompt'
import { getAddressList, addressUpdate } from '@/api/address'

export default {
  components: { Pagination, Dialog, detail, remarkPropt },
  filters: {
    // statusFilter(status) {
    //   const statusMap = {
    //     published: 'success',
    //     draft: 'gray',
    //     deleted: 'danger'
    //   }
    //   return statusMap[status]
    // },
    // 等于之前的level
    formatvipId(vipId) {
      const vipIdMap = {
        1: '泰山美妈',
        2: '泰山美妈团',
        3: '泰山美妈社',
        4: '泰山美妈会'
      }
      return vipIdMap[vipId]
    },

    nullStr(str) {
      if (str === '' || str === null) {
        return '-'
      } else {
        return str
      }
    }
  },
  data() {
    return {
      // visible: false,
      remarkProptVisible: false,
      remarkProptList: [],
      remarkProptIndex: -1,
      userName: '',
      userType: -1,
      //   showDialog: false,
      //   courseSection: '', // 课程金额
      //   bankName: '', // 银行名称
      //   bankId: '', // 银行Id
      //   orderId: '', // 订单Id
      //   type: 1, // 1:提课程款. 2:退佣金
      //   tid: '',
      //   courseTitle: '新增课程',
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        pageNum: 0,
        pageSize: 10
      },
      search: {
        orderNumbers: '',
        vipId: '',
        name: '',
        phone: '',
        startTime: '',
        endTime: ''
      },
      dialogFormVisible: false,
      course: {
        courseName: '',
        sign: '',
        introduce: '',
        address: '',
        imgUrl: '',
        courseType: '',
        teacher: '',
        courseTime: '',
        startTime: '',
        endTime: ''
      },
      dialogTableVisible: false,
      detailIndex: -1,
      permissionShow: false // 权限数据未到达之前按钮隐藏
      // permissionList: [] // 权限数据列表
    }
  },
  computed: {
    // permissionBoolean() {
    //   return function(str) {
    //     const arr = []
    //     for (let i = 0; i < this.permissionList.length; i++) {
    //       arr.push(this.permissionList[i].name)
    //     }
    //     const index = arr.indexOf(str)

    //     if (index !== -1) {
    //       return true
    //     } else {
    //       return false
    //     }
    //   }
    // }
  },
  watch: {
    'permissionList'(newVal) {
      this.permissionList = newVal
      this.permissionShow = true
    }
  },
  created() {
    this.getUserType()
    this.fetchData()
    // this.getPermissonFn()
  },
  methods: {
    // 权限数据的获取
    // getPermissonFn() {
    //   const id = getLocalId()
    //   const uid = getLocalUid()
    //   const data = { uid }
    //   getPermission(id, data).then(res => {
    //     console.log('权限结果', res)
    //     if (res.code === '200') {
    //       this.permissionList = res.data
    //       // console.log('权限名称：', this.getPermissionName(this.permissionList))
    //     } else {
    //       Message.error(res.message + '-' + res.data)
    //     }
    //   })
    // },
    // 备注
    changeRemarks(uid, id, index) {
      this.remarkProptIndex = index
      console.log('--->', uid, id, index)
      const userData = ''
      this.remarkProptVisible = true
      this.$refs.remarke.request(uid, id)
    },
    remarkText() {
      this.fetchData()
    },
    getUserType() {
      switch (getLocalUserName()) {
        case 'admin':
          this.userType = 1
          break;
        case 'sale':
          this.userType = 2
          break;
        case 'casher':
          this.userType = 3
          break;
      }
    },
    // 查询
    // toSearch() {
    //   if (this.search.startTime) this.search.startTime = this.search.startTime + ' 00:00:00'
    //   if (this.search.endTime) this.search.endTime = this.search.endTime + ' 23:23:59'
    //   this.listLoading = true
    //   console.log(this.search)
    //   const newData = this.objectCon(this.listQuery, this.search)
    //   // console.log(this.search.startTime)
    //   // console.log(this.search.endTime)
    //   getAddressList(newData).then(response => {
    //     this.list = response.data.list
    //     this.total = response.data.total
    //     setTimeout(() => {
    //       this.listLoading = false
    //     }, 0.5 * 1000)
    //   })
    // },
    // 重置
    // toReset() {
    //   this.search.orderNumbers = ''
    //   this.search.vipId = ''
    //   this.search.name = ''
    //   this.search.phone = ''
    //   this.search.startTime = ''
    //   this.search.endTime = ''
    // },
    // isDialog(a) {
    //   this.visible = false
    //   this.showDialog = false
    // },
    fetchData() {
      this.listLoading = true
      const newData = this.objectCon(this.listQuery)
      console.log('newData:=>', newData)
      getAddressList(newData).then(response => {
        if (response.code === '200') {
          this.list = response.data.list
          console.log('订单数据：', this.list)
          this.total = response.data.total
          setTimeout(() => {
            this.listLoading = false
          }, 0.5 * 1000)
        } else {
          this.listLoading = false
        }
      })
    },
    see(index) {
      this.dialogTableVisible = true
      this.detailIndex = index
    },
    edit(row) {
      const query = { isEdit: true, data: row }
      this.$router.push({
        path: '/service/add',
        query
      })
    },
    update(id, type) {
      const strAsk = type === '0' ? '是否停用？' : '是否启用？'
      const str = type === '0' ? '已停用' : '已启用'
      this.$confirm(strAsk, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(res => {
        const data = { status: type, id }
        addressUpdate(data).then(res => {
          if (res.code === '200') {
            Message.success(str)
            this.fetchData()
          } else {
            Message.error(res.message + res.data)
          }
        })
      })
    }
    // submit(oid, tid, courseSection) {
    //   const self = this;
    //   selectCard(tid).then(res => {
    //     // console.log(res.data)
    //     if (!(res.code === '200')) return false;
    //     if (res.data.id == null) {
    //       // 未获取到银行卡信息
    //       self.$message({
    //         showClose: true,
    //         message: '未绑定银行卡信息，请先绑定！',
    //         type: 'warning'
    //       });
    //       setTimeout(() => {
    //         this.$refs.bankDialog.getMsg(tid)
    //         self.visible = true
    //       }, 0.5 * 1000)
    //     } else {
    //       // 已获取到银行卡信息
    //       self.type = 1
    //       this.$refs.dialog.changeText(self.type)
    //       self.bankId = res.data.id
    //       self.orderId = oid
    //       self.bankName = res.data.bankName
    //       self.courseSection = courseSection
    //       self.tid = tid
    //       self.showDialog = true
    //     }
    //   })
    // },
    // refund(oid, tid, orderCommission) {
    //   const self = this;
    //   selectCard(tid).then(res => {
    //     if (!(res.code === '200')) return false;
    //     if (res.data == null) {
    //       // 未获取到银行卡信息
    //       self.$message({
    //         showClose: true,
    //         message: '未绑定银行卡信息，请先绑定！',
    //         type: 'warning'
    //       });
    //       setTimeout(() => {
    //         this.$refs.bankDialog.getMsg(tid)
    //         self.visible = true
    //       }, 0.5 * 1000)
    //     } else {
    //       // 已获取到银行卡信息
    //       // console.log(res.data)
    //       self.type = 2
    //       this.$refs.dialog.changeText(self.type)
    //       self.bankId = res.data.id
    //       self.orderId = oid
    //       self.bankName = res.data.bankName
    //       self.courseSection = orderCommission
    //       self.tid = tid
    //       self.showDialog = true
    //     }
    //   })
    // },
    // edit(pid, index) {
    //   this.$data.dialogFormVisible = true
    //   this.$data.courseTitle = '编辑课程'
    //   this.$data.course = this.$data.list[index]
    // },
    // getValue(value) {
    //   this.$data.course.status = value
    // }
  }
}
</script>
<style lang="scss" scoped>
  .inputBox{
    div{ margin-bottom: 10px;}
  }
</style>
